﻿namespace WMS6.Web.TaskMgmt
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.TaskMgtServiceRef;
    using WMS6.Web.UserControls;

    public class TaskList : BasePage
    {
        protected Button btnDelete;
        protected Button btnEdit;
        protected Button btnExport;
        protected Button btnList;
        protected Button btnNew;
        protected Button btnRefurbish;
        protected Button btnReSearch;
        protected Button btnSearch;
        protected HtmlForm F6012;
        protected GridView gvTask;
        protected HtmlInputHidden hdnActionID;
        protected HtmlInputHidden hdnFrom;
        protected HtmlHead Head1;
        protected Label lblMsg;
        protected Label lblUom;
        protected HtmlLink mycss;
        protected Pager PagerFooter;
        protected ScriptManager ScriptManager1;
        protected UpdatePanel up1;

        private void BindDataTaskQueryList(TaskDetailQueryEntity taskQuery)
        {
            this.PagerFooter.CurrentPage = int.Parse(taskQuery.CurrentPage);
            DataSet ds = new DataSet();
            ds = TaskMgtServiceClientProxy.GetInstance().GetTaskDetailByQueryList(taskQuery);
            base.BindData2GridView(this.gvTask, ds);
            if ((ds != null) && (ds.Tables.Count > 1))
            {
                this.PagerFooter.PageInfo = ds.Tables[1].Rows[0]["CurrentPage"].ToString() + " / " + ds.Tables[1].Rows[0]["TotalPagesCount"].ToString() + "&nbsp;&nbsp;&nbsp;共: " + ds.Tables[1].Rows[0]["TotalRecordsCount"].ToString();
                this.PagerFooter.TotalPages = int.Parse(ds.Tables[1].Rows[0]["TotalPagesCount"].ToString());
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                List<string> selectedID = base.GetSelectedIDsInGridView(this.gvTask, "TASK_DETAIL_ID");
                List<string> statuslist = base.GetSelectedIDsInGridView(this.gvTask, "STATUS");
                if ((selectedID == null) || (selectedID.Count == 0))
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("C008");
                }
                else
                {
                    for (int i = 0; i < selectedID.Count; i++)
                    {
                        if (statuslist[i].ToUpper() == "COMPLETED")
                        {
                            this.lblMsg.Text = base.GetGlobalMsgResource("F6050_002");
                            return;
                        }
                    }
                    this.lblMsg.Text = TaskMgtServiceClientProxy.GetInstance().DeleteTaskDetail(selectedID) + base.GetGlobalMsgResource("C006");
                    TaskDetailQueryEntity taskEntity = this.GetTaskQueryEntity();
                    this.BindDataTaskQueryList(taskEntity);
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C007") + ex.Message;
            }
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            List<string> selectedID = base.GetSelectedIDsInGridView(this.gvTask, "TASK_DETAIL_ID");
            if ((selectedID == null) || (selectedID.Count == 0))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C008");
            }
            else if (selectedID.Count > 1)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C009");
            }
            else
            {
                base.Response.Redirect(string.Format("Task.aspx?from=list&ID={0}", base.UrlEncode(selectedID[0])));
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            TaskDetailQueryEntity taskEntity = this.GetTaskQueryEntity();
            taskEntity.IsGetAll = true;
            this.hdnActionID.Value = "Y";
            string currentPageIndex = taskEntity.CurrentPage;
            taskEntity.CurrentPage = "0";
            this.gvTask.Columns[1].Visible = false;
            this.gvTask.AllowSorting = false;
            this.BindDataTaskQueryList(taskEntity);
            this.hdnActionID.Value = "";
            this.gvTask.ShowFooter = false;
            taskEntity.IsGetAll = false;
            taskEntity.CurrentPage = currentPageIndex;
            this.SetTaskQueryEntity(taskEntity);
            base.Export2Excel("Task.xls", this.gvTask);
        }

        protected void btnFirst_Click(object sender, EventArgs e)
        {
            TaskDetailQueryEntity taskEntity = this.GetTaskQueryEntity();
            taskEntity.CurrentPage = "0";
            this.BindDataTaskQueryList(taskEntity);
        }

        protected void btnLast_Click(object sender, EventArgs e)
        {
            TaskDetailQueryEntity taskEntity = this.GetTaskQueryEntity();
            taskEntity.CurrentPage = (this.PagerFooter.TotalPages - 1).ToString();
            this.BindDataTaskQueryList(taskEntity);
        }

        protected void btnList_Click(object sender, EventArgs e)
        {
            TaskDetailQueryEntity taskDetailEntity = this.GetTaskQueryEntity();
            taskDetailEntity.StartTime = this.PreviousPage.BeginTime;
            taskDetailEntity.EndTime = this.PreviousPage.EndTime;
            taskDetailEntity.FromLoc = this.PreviousPage.FromLoc;
            taskDetailEntity.FromLpnID = this.PreviousPage.FromLpnID;
            taskDetailEntity.Priority = this.PreviousPage.Priority;
            taskDetailEntity.Qty = this.PreviousPage.Qty;
            taskDetailEntity.SkuID = this.PreviousPage.SkuID;
            taskDetailEntity.TaskDetailID = this.PreviousPage.TaskDetailID;
            taskDetailEntity.TaskType = this.PreviousPage.TaskType;
            taskDetailEntity.ToLoc = this.PreviousPage.ToLoc;
            taskDetailEntity.ToLpnID = this.PreviousPage.ToLpnID;
            taskDetailEntity.UserID = this.PreviousPage.UserID;
            this.BindDataTaskQueryList(taskDetailEntity);
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            TaskDetailQueryEntity taskEntity = this.GetTaskQueryEntity();
            if (int.Parse(taskEntity.CurrentPage) < (this.PagerFooter.TotalPages - 1))
            {
                taskEntity.CurrentPage = (int.Parse(taskEntity.CurrentPage) + 1).ToString();
                this.BindDataTaskQueryList(taskEntity);
            }
        }

        protected void btnPrevious_Click(object sender, EventArgs e)
        {
            TaskDetailQueryEntity taskEntity = this.GetTaskQueryEntity();
            if (int.Parse(taskEntity.CurrentPage) > 0)
            {
                taskEntity.CurrentPage = (int.Parse(taskEntity.CurrentPage) - 1).ToString();
                this.BindDataTaskQueryList(taskEntity);
            }
        }

        private TaskDetailQueryEntity GetTaskQueryEntity()
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                return (base.PageQueryEntityViewState as TaskDetailQueryEntity);
            }
            return (base.PageQueryEntitySession as TaskDetailQueryEntity);
        }

        protected void GoTo_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(((TextBox) this.PagerFooter.FindControl("txtGo2Page")).Text.Trim()))
            {
                TaskDetailQueryEntity taskEntity = this.GetTaskQueryEntity();
                taskEntity.CurrentPage = this.PagerFooter.GoToPage.ToString();
                this.BindDataTaskQueryList(taskEntity);
            }
        }

        protected void gvTask_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F6012", this.gvTask);
        }

        protected void gvTask_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (((this.PagerFooter.CurrentPage * this.PagerFooter.PageSize) + e.Row.RowIndex) + 1).ToString();
                e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
                ((CheckBox) e.Row.FindControl("chbSelect")).Attributes.Add("onclick", "GridViewCheckBoxClick('" + e.Row.ClientID + "')");
                if (!string.IsNullOrEmpty(this.hdnActionID.Value))
                {
                    ((HyperLink) e.Row.FindControl("HyperLink1")).Enabled = false;
                }
            }
        }

        protected void gvTask_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                TaskDetailQueryEntity taskEntity = this.GetTaskQueryEntity();
                taskEntity.SortField = e.SortExpression;
                if (taskEntity.SortDirection == "ASC")
                {
                    taskEntity.SortDirection = "DESC";
                }
                else if (taskEntity.SortDirection == "DESC")
                {
                    taskEntity.SortDirection = "ASC";
                }
                this.BindDataTaskQueryList(taskEntity);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void LoadData()
        {
            TaskDetailQueryEntity taskDetailEntity = new TaskDetailQueryEntity();
            if (string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                if (this.PreviousPage != null)
                {
                    taskDetailEntity.StartTime = this.PreviousPage.BeginTime;
                    taskDetailEntity.EndTime = this.PreviousPage.EndTime;
                    taskDetailEntity.FromLoc = this.PreviousPage.FromLoc;
                    taskDetailEntity.FromLpnID = this.PreviousPage.FromLpnID;
                    taskDetailEntity.Priority = this.PreviousPage.Priority;
                    taskDetailEntity.Qty = this.PreviousPage.Qty;
                    taskDetailEntity.SkuID = this.PreviousPage.SkuID;
                    taskDetailEntity.TaskDetailID = this.PreviousPage.TaskDetailID;
                    taskDetailEntity.TaskType = this.PreviousPage.TaskType;
                    taskDetailEntity.ToLoc = this.PreviousPage.ToLoc;
                    taskDetailEntity.ToLpnID = this.PreviousPage.ToLpnID;
                    taskDetailEntity.UserID = this.PreviousPage.UserID;
                }
            }
            else
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            taskDetailEntity.IsGetAll = false;
            taskDetailEntity.CurrentPage = "0";
            taskDetailEntity.PageSize = this.gvTask.PageSize.ToString();
            taskDetailEntity.SortField = "TASK_DETAIL_ID";
            taskDetailEntity.SortDirection = "DESC";
            this.PagerFooter.PageSize = this.gvTask.PageSize;
            if (!string.IsNullOrEmpty(base.Request.QueryString["export"]))
            {
                this.hdnActionID.Value = base.Request.QueryString["export"];
            }
            this.SetTaskQueryEntity(taskDetailEntity);
            this.BindDataTaskQueryList(taskDetailEntity);
            if (!string.IsNullOrEmpty(base.Request.QueryString["export"]))
            {
                this.btnExport_Click(null, null);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetTaskQueryEntity(TaskDetailQueryEntity taskQuery)
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                base.PageQueryEntityViewState = taskQuery;
            }
            else
            {
                base.PageQueryEntitySession = taskQuery;
            }
        }

        public TaskQuery PreviousPage
        {
            get
            {
                return (TaskQuery) base.PreviousPage;
            }
        }
    }
}

